//
//  SmartLifeListViewController.m
//  SmartLife
//
//  Created by Lawrence on 15/9/29.
//  Copyright © 2015年 Lawrence. All rights reserved.
//

#import "SmartLifeListViewController.h"
#import "SmartLifeList.h"
#import "WeatherViewController.h"
#import "PlistHelper.h"
#import "CityWeatherList.h"
#import "MapViewController.h"
#import "PhoneQueryViewController.h"
#import <RESideMenu.h>
#import <KxMenu.h>
#import "QRCodeViewController.h"
#import "NotesViewController.h"


//#define
#define SYSTEM_VERSION_LESS_THAN(v)([[[UIDevice currentDevice] systemVersion] compare:voptions:NSNumericSearch]==NSOrderedAscending) //用来获取手机的系统，判断版本是多少
@interface SmartLifeListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray *cellItems;
    NSArray *cellName;
    NSArray *cellImage;
    
}
@end
@implementation SmartLifeListViewController

-(instancetype)init{
    self=[super init];
    if (self) {
        UINavigationItem *navItem=self.navigationItem;
        navItem.title=NSLocalizedString(@"SmartLife",nil);
        UIBarButtonItem *bbi=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(initKxMenu:)];
        navItem.rightBarButtonItem=bbi;
        
        
        //设置tag用于回收和打开下拉菜单
        navItem.rightBarButtonItem.tag=10;
        
        UIBarButtonItem *lbbi=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
        navItem.leftBarButtonItem=lbbi;
        
        [self addGesture];
        
        
        cellName=@[
                   NSLocalizedString(@"天气",nil),
                   NSLocalizedString(@"地图",nil),
                   NSLocalizedString(@"电话归属地查询",nil),
                   NSLocalizedString(@"二维码",nil),
                   NSLocalizedString(@"便签",nil)
                   ];
        
        cellImage=@[
                    @"weather",
                    @"map",
                    @"search",
                    @"QrCode",
                    @"EventNote"
                    ];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //读取xib文件
    [self registerCellNib];
    self.tableView.delegate=self; //设置tableview的代理
    self.tableView.dataSource=self;  //设置tableview的数据源
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    self.tableView.frame=CGRectMake(0,self.view.frame.size.height*.1,self.view.frame.size.width,self.view.frame.size.height*.9);
    [self.tableView sizeToFit];
    
    
    [self.SearchBar  setFrame:CGRectMake(0,0,self.view.frame.size.width,60)];
    [self.view addSubview:_tableView];
}
-(void) registerCellNib{
    UINib *nib=[UINib nibWithNibName:@"SmartLifeListCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"SmartLifeListCell"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 设置每个section有多少行
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
#pragma mark 设置返回的行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SmartLifeList *cell=[tableView dequeueReusableCellWithIdentifier:@"SmartLifeListCell"];
    cell.labName.text=[cellName objectAtIndex:indexPath.section];
    cell.ImageTitle.image=[UIImage imageNamed:[cellImage objectAtIndex:indexPath.section]];
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0f;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 3.0f;
}
#pragma mark 选择事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0&&indexPath.row==0) {
        NSDictionary *dict=[PlistHelper GetCityInfoCode];
        NSDictionary *dictCountry=[dict objectForKey:@"China"];
        NSDictionary *dictGuangDong=[dictCountry objectForKey:@"GuangDong"];
        //CityWeatherList *cityList=[CityWeatherList initCityWeatherListWithStoryboard:dictGuangDong];
       // [self.navigationController pushViewController:cityList animated:NO];
    }
    else if(indexPath.section==1&&indexPath.row==0){
        self.viewController=[[AMapNavViewController alloc] init];
        [self.navigationController pushViewController:self.viewController animated:YES];
    }
    else if(indexPath.section==2&&indexPath.row==0){
        PhoneQueryViewController *pQC=[[PhoneQueryViewController alloc] init];
        [self.navigationController pushViewController:pQC animated:NO];
    }else if(indexPath.section==3&&indexPath.row==0){
        QRCodeViewController *qrView=[[QRCodeViewController alloc] init];
        [self.navigationController pushViewController:qrView animated:NO];
    }else if(indexPath.section==4&&indexPath.row==0){
        NotesViewController *notesView=[[NotesViewController alloc] initWithStyle:UITableViewStyleGrouped];
        [self.navigationController pushViewController:notesView animated:NO];
    }
}
#pragma mark 添加手势
-(void)addGesture{
    UIPanGestureRecognizer *panGestureRecognizer=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(presentRightMenuViewController:)];
    [self.tableView addGestureRecognizer:panGestureRecognizer];
    
    //添加点击手势
    UITapGestureRecognizer *tapGestureRec=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SetNavItemTag)];
    tapGestureRec.cancelsTouchesInView=NO;
    [self.view addGestureRecognizer:tapGestureRec];
}

-(void)initKxMenu:(id)sender{
    if (self.navigationItem.rightBarButtonItem.tag==10) {
        NSArray *menuItems=@[
                             [KxMenuItem menuItem:@"扫一扫" image:[UIImage imageNamed:@"qr_BarCode"] target:self action:@selector(pushQrCodeViewController)],
                             [KxMenuItem menuItem:@"快递单号" image:[UIImage imageNamed:@"BarCode"] target:self action:nil]
                             ];
        for (KxMenuItem *item in menuItems) {
            item.foreColor=[UIColor colorWithRed:47/255.0f green:112/255.0f blue:255/255.0f alpha:1.0];
            item.alignment=NSTextAlignmentCenter;
        }
        
        CGRect rect=self.navigationController.navigationBar.frame;
        CGFloat height=0.0;
        //判断系统版本号为多少
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
            height=64.0;
        }
        else{
            height=0.0;
        }
        [KxMenu showMenuInView:self.view fromRect:CGRectMake(rect.origin.x+self.navigationController.navigationBar.frame.size.width/2-44, rect.origin.y-10,rect.size.width,height) menuItems:menuItems];
        self.navigationItem.rightBarButtonItem.tag=20;
    }
    else{
       [KxMenu dismissMenu];
        self.navigationItem.rightBarButtonItem.tag=10;
    }
}
-(void)pushQrCodeViewController{
    QRCodeViewController *qrView=[[QRCodeViewController alloc] init];
    [self.navigationController pushViewController:qrView animated:YES];
}
-(void)SetNavItemTag{
    self.navigationItem.rightBarButtonItem.tag=10;
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    [self.tableView setFrame:[[UIScreen mainScreen] bounds]];
}


@end
